
import java.io.BufferedReader; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;


public class playFair_old {
	public static void main(String args[])
	{
		String keyword=null,ptext=null,cipherText=null,string=null,string1=null;
		int Key=0,count=0,tableCount=0;
		char[][] table=new char[6][6];
		int[] chkRepeat=new int[26];
		int[] intchkRepeat=new int[10];
		int[] matrixIndices=null;
		int[] eleLocation=new int[26];
		int[] DeceleLocation=new int[26];
		String[] splitPtext=null;
		ArrayList ptxt=new ArrayList();
		ArrayList ctxt=new ArrayList();
		ArrayList Newptxt=new ArrayList();
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		try {
			System.out.println("Enter the Keyword");
			keyword=br.readLine();
			
			for(int i=0;i<26;i++)
			{
				chkRepeat[i]=0;
			}
			for(int i=0;i<10;i++)
			{
				chkRepeat[i]=0;
			}
			
			for(int i=0;i<6;i++)
			{
				for(int j=0;j<6;j++)
				{
					if(count<keyword.length())
					{
						if(keyword.charAt(count)==' ' || chkRepeat[keyword.charAt(count)-97]==1)
						{
							count++;
							j--;
						}
						else if(count<keyword.length())
						{
						table[i][j]=keyword.charAt(count);
						chkRepeat[(int)keyword.charAt(count)-97]=1;
						count++;
						tableCount++;
						}
					}
					else if(tableCount>25)
					{
						for(int s=0;s<10;s++)
						{
							if(intchkRepeat[s]==0)
							{
								table[i][j]=(char) (s+48);
								intchkRepeat[s]=1;
								break;
							}
						}
					}
					else
						for(int k=0;k<26;k++)
						{
							if(chkRepeat[k]==0)
							{
								table[i][j]=(char) (97+k);
								chkRepeat[k]=1;
								tableCount++;
								break;
							}
						}
				}
				
			}
			
			for(int i=0;i<6;i++)
			{
				for(int j=0;j<6;j++)
				{
					
					System.out.print(" table["+i+"]"+"["+j+"]= "+table[i][j]+"\t");
				}
				System.out.println("");
			}
			System.out.println("Enter the PlainText");
			ptext=br.readLine();
			for(int ListCount=0;ListCount<ptext.length();ListCount++)
			{
				ptxt.add(ptext.charAt(ListCount)+"");
				if(ListCount!=0 && ptxt.get(ListCount).equals(ptxt.get(ListCount-1)) && ListCount%2!=0 )
				{
					ptxt.add(ListCount,"x");
				}
			}
			if(ptxt.size()%2!=0)
			{
				ptxt.add("x");
			}
			{
				System.out.println(ptxt);
			}
			int l=0,n=0;
			matrixIndices=new int[ptxt.size()];
			for(int i=0;i<6;i++)
			{
				for(int j=0;j<6;j++)
				{
					for(int h=0;h<ptxt.size();h++)
					{
						
							string=(String) ptxt.get(h);
							if(chkRepeat[((char)(string.charAt(0))-97)]==0)
							{
								matrixIndices[h]=eleLocation[(char)(string.charAt(0))-97];
							}
							
							else if(ptxt.get(h).equals(table[i][j]+""))
							{
								matrixIndices[h]=i*6+j;
								chkRepeat[table[i][j]-97]=0;
								eleLocation[table[i][j]-97]=i*6+j;
							}
					}
				}
				
			}
			int rowCurrent=0,colCurrent=0,row=0,col=0;
			for(int i=0;i<matrixIndices.length;i++)
			{
					row=matrixIndices[i]/6;
					col=matrixIndices[i]%6;
					System.out.print("["+row+"]"+"["+col+"] ="+matrixIndices[i]+",");
			}
			System.out.println("Ciphertext is: "+ctxt);	
			//row=matrixIndices%6
			//col=matrixIndices/6
			row=0;
			col=0;
			for(int i=0;i<ptxt.size();i++)
			{
				
				if(i%2==0)
				{
					string=(String) ptxt.get(i);
					string1=(String) ptxt.get(i+1);
					rowCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
					colCurrent=eleLocation[(char)(string.charAt(0))-97]%6;
					row=eleLocation[(char)(string1.charAt(0))-97]/6;
					col=eleLocation[(char)(string1.charAt(0))-97]%6;
					if(rowCurrent==row)
					{
						ctxt.add(i,""+table[rowCurrent][(colCurrent+1)%6]);
						ctxt.add(i+1,""+table[row][(col+1)%6]);
						i++;
					}
					else if(colCurrent==col)
					{
						ctxt.add(i,""+table[(rowCurrent+1)%6][(colCurrent)]);
						ctxt.add(i+1,""+table[(row+1)%6][col]);
						i++;
					}
					else
					{
						ctxt.add(i,""+table[rowCurrent][col]);
					}
				}
				else if(i%2==1)
				{
					string=(String) ptxt.get(i);
					string1=(String) ptxt.get(i-1);
					rowCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
					colCurrent=eleLocation[(char)(string.charAt(0))-97]/6;
					row=eleLocation[(char)(string1.charAt(0))-97]/6;
					col=eleLocation[(char)(string1.charAt(0))-97]%6;
					ctxt.add(i,""+table[rowCurrent][col]);
				}
			}
			System.out.println("Ciphertext is: "+ctxt);
			////////////////////////////////////////Decryption//////////////////////////////////////////////////
			
			
			
			row=0;
			col=0;
			rowCurrent=0;
			colCurrent=0;
			for(int i=0;i<6;i++)
			{
				for(int j=0;j<6;j++)
				{
					for(int h=0;h<ctxt.size();h++)
					{
						
							string=(String) ctxt.get(h);
							if(chkRepeat[((char)(string.charAt(0))-97)]==0)
							{
								matrixIndices[h]=DeceleLocation[(char)(string.charAt(0))-97];
							}
							
							else if(ctxt.get(h).equals(table[i][j]+""))
							{
								matrixIndices[h]=i*6+j;
								chkRepeat[table[i][j]-97]=0;
								DeceleLocation[table[i][j]-97]=i*6+j;
							}
					}
				}
				
			}
			
			for(int i=0;i<ctxt.size();i++)
			{
				
				if(i%2==0)
				{
					string=(String) ctxt.get(i);
					string1=(String) ctxt.get(i+1);
					rowCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
					colCurrent=DeceleLocation[(char)(string.charAt(0))-97]%6;
					row=DeceleLocation[(char)(string1.charAt(0))-97]/6;
					col=DeceleLocation[(char)(string1.charAt(0))-97]%6;
					if(rowCurrent==row)
					{
						Newptxt.add(i,""+table[rowCurrent][(colCurrent+1)%6]);
						Newptxt.add(i+1,""+table[row][(col+1)%6]);
						i++;
						System.out.println("Plaintext is: "+Newptxt);
					}
					else if(colCurrent==col)
					{
						Newptxt.add(i,""+table[(rowCurrent+1)%6][(colCurrent)]);
						Newptxt.add(i+1,""+table[(row+1)%6][col]);
						i++;
						System.out.println("Plaintext is: "+Newptxt);
					}
					else
					{
						Newptxt.add(i,""+table[rowCurrent][col]);
						System.out.println("Plaintext is: "+Newptxt);
					}
				}
				else if(i%2==1)
				{
					string=(String) ctxt.get(i);
					string1=(String) ctxt.get(i-1);
					rowCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
					colCurrent=DeceleLocation[(char)(string.charAt(0))-97]/6;
					row=DeceleLocation[(char)(string1.charAt(0))-97]/6;
					col=DeceleLocation[(char)(string1.charAt(0))-97]%6;
					Newptxt.add(i,""+table[rowCurrent][col]);
					System.out.println("Plaintext is: "+Newptxt);
				}
			}
			System.out.println("Plaintext is: "+Newptxt);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Exception:"+e.getCause()+" ------ "+e.getLocalizedMessage()+" ------ "+e.getMessage());
		}
		
	}
}
